package com.UI;

import com.Model.StudentDBModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterUser extends Activity implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_page);

		// register this activity as a button event listener
		Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
		btnSubmit.setOnClickListener(this);

	}

	/**
	 * This method is called when the user clicks the Submit button. It sends
	 * the data to the model, to be added to persistent storage.
	 */
	@Override
	public void onClick(View component) {
		
 		EditText txtFname = (EditText)findViewById(R.id.txtFirstName);
		EditText txtLname = (EditText)findViewById(R.id.txtLastName);
		
		String tempFname = txtFname.getText().toString().replaceAll(" ", "");
		String tempLname = txtLname.getText().toString().replaceAll(" ", "");
		
		if(tempFname.length() == 0) {
  	      
 		   AlertDialog alertDialog = new AlertDialog.Builder(RegisterUser.this).create();	        			
 		   alertDialog.setTitle("Warning");	        		  
 		   alertDialog.setMessage("Please Enter your First Name");	        		  	        		  
 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
 		  
 		   public void onClick(DialogInterface dialog, int which) {
		       return;
		  } }); 
		  alertDialog.show();
		  
		}
		
		else if(tempLname.length() == 0 ) {
	  	      
	 		   AlertDialog alertDialog = new AlertDialog.Builder(RegisterUser.this).create();	        			
	 		   alertDialog.setTitle("Warning");	        		  
	 		   alertDialog.setMessage("Please Enter your Last Name");	        		  	        		  
	 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
	 		  
	 		   public void onClick(DialogInterface dialog, int which) {
			       return;
			  } }); 
			  alertDialog.show();
			  
	   }
		
	   else {
		   
		// create a model instance
		StudentDBModel newAccount = new StudentDBModel(this);

		// Get the values from the user and send them to the DB
		ContentValues values = new ContentValues();
		EditText txtFirstName = (EditText) findViewById(R.id.txtFirstName);
		values.put("FirstName", txtFirstName.getText().toString());

		EditText txtLastName = (EditText) findViewById(R.id.txtLastName);
		values.put("LastName", txtLastName.getText().toString());

		EditText txtMajor = (EditText) findViewById(R.id.txtMajor);
		values.put("Major", txtMajor.getText().toString());

		EditText txtUniversity = (EditText) findViewById(R.id.txtUniversity);
		values.put("University", txtUniversity.getText().toString());

		// use the model to store the data
		newAccount.addNewAccount(values);
		//adding default value for preferences
		newAccount.addPreferences();

		// tell the user the user account was added to the database
		int numUsers = newAccount.getNumUsers();
		TextView lblResults = (TextView) findViewById(R.id.lblAddResult);
		lblResults.setText(numUsers + getString(R.string.YesAdded));

		//once a user is registered, go to the CourseActivity View
		Intent home = new Intent(RegisterUser.this, HomePage.class);
		startActivity(home);
		RegisterUser.this.finish();		
		
		}
	}

}
